package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.po.AuthInfo;
import net.wanho.service.AuthInfoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final AuthInfoService authInfoService;

    // 权限树
    @GetMapping("/auth-tree")
    public R getAuthTree() {
        List<AuthInfo> menu = authInfoService.getMenu();
        System.out.println(menu);
        return R.ok(menu);
    }

    // 权限名称查询
    @GetMapping("/name-check")
    public R nameCheck(String authName) {
        Boolean authInfo = authInfoService.findByAuthName(authName);
        System.out.println(authInfo + "authInfo");
        return R.ok("success", authInfo);
    }

    // 权限url查询
    @GetMapping("/url-check")
    public R urlCheck(String authUrl) {
        Boolean authInfo = authInfoService.findByAuthUrl(authUrl);

        return R.ok(authInfo);
    }

    // 权限编码查询
    @GetMapping("/code-check")
    public R codeCheck(String authCode) {
        Boolean authInfo = authInfoService.findByAuthCode(authCode);
        return R.ok(authInfo);
    }

    // 权限添加
    @PostMapping("/auth-add")
    public R authAdd(@RequestBody AuthInfo authInfo) {
        // TODO 可以添加到数据库 查询无法查到 查询可能有问题 除此以外功能都已实现（大概）可能还有bug
        authInfoService.add(authInfo);
        return R.ok("权限添加成功！");
    }

    // 权限启用
    @PutMapping("/auth-enable/{id}")
    public R authEnable(@PathVariable Integer id) {
        authInfoService.authEnable(id);
        return R.ok("权限启用成功！");
    }

    // 权限禁用
    @PutMapping("/auth-disable/{id}")
    public R authDisable(@PathVariable Integer id) {
        authInfoService.authDisable(id);
        return R.ok("权限禁用成功！");
    }

    // 权限修改
    @PutMapping("/auth-update")
    public R authUpdate(@RequestBody AuthInfo authInfo) {
        authInfoService.updateAuthInfo(authInfo);
        return R.ok(("权限修改成功！"));
    }

    // 权限删除
    @DeleteMapping("/auth-delete/{id}")
    public R authDelete(@PathVariable Integer id) {
        authInfoService.authDelete(id);
        return R.ok("权限删除成功！");
    }

}
